package _21_多线程juc.demo01.safeend;

/**
 * 中断 interrupt()
 *      不会立即终止，只是修改了标记位
 *
 * 需要终止 A线程时，
 *      可以先调用 A.interrupt() 方法
 *      然后在 A线程中 检查
 *
 * @author liuhuan
 */
public class EndThread_1 extends Thread {

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + " ----- interrupt flog = " + isInterrupted());
        while (!isInterrupted()) { // 需要手动检测
            System.out.println(name + " is running");
            System.out.println(name + " interrupt flog = " + isInterrupted());
        }

        // 释放资源
        // xxx

        System.out.println(name + " ----- interrupt flog = " + isInterrupted());
    }

    public static void main(String[] args) throws InterruptedException {
        EndThread_1 endThread1 = new EndThread_1();
        endThread1.start();

        Thread.sleep(10);
        endThread1.interrupt();// 设置中断标记位 为 true
    }
}
